home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 9866 / 9866.xpi / components / samfind_autocomplete.js
Text File  |  2009-09-08  |  4KB  |  186 lines

  1. //-------------------------------------------------------------------------------------------
  2. // Autocomplete component.
  3. // See:
  4. //    https://developer.mozilla.org/en/How_to_implement_custom_autocomplete_search_component
  5. //-------------------------------------------------------------------------------------------
  6.  
  7. const Ci = Components.interfaces;
  8. const Cr = Components.results;
  9.  
  10. function samfindAutoCompleteResult(searchString,
  11.                                    searchResult,
  12.                                    defaultIndex,
  13.                                    errorDescription,
  14.                                    images,
  15.                                    results,
  16.                                    comments)
  17. {
  18.     this._searchString = searchString;
  19.     this._searchResult = searchResult;
  20.     this._defaultIndex = defaultIndex;
  21.     this._errorDescription = errorDescription;
  22.     this._images = images;
  23.     this._results = results;
  24.     this._comments = comments;
  25. }
  26.  
  27. samfindAutoCompleteResult.prototype =
  28. {
  29.     _searchString : "",
  30.     _searchResult : 0,
  31.     _defaultIndex : 0,
  32.     _errorDescription : "",
  33.     _images : [],
  34.     _results : [],
  35.     _comments : [],
  36.  
  37.     get searchString() {return this._searchString;},
  38.     get searchResult() {return this._searchResult;},
  39.     get defaultIndex() {return this._defaultIndex;},
  40.     get errorDescription() {return this._errorDescription;},
  41.     get matchCount() {return this._results.length;},
  42.     getImageAt : function(index)
  43.     {
  44.         return this._images[index];
  45.     },
  46.     getCommentAt : function(index)
  47.     {
  48.         return this._comments[index];
  49.     },
  50.     getValueAt : function(index)
  51.     {
  52.         return this._results[index];
  53.     },
  54.     getStyleAt : function(index)
  55.     {
  56.         return null;
  57.     },
  58.     removeValueAt : function(index, removeFromDb)
  59.     {
  60.         this._images.splice(index, 1);
  61.         this._results.splice(index, 1);
  62.         this._comments.splice(index, 1);
  63.     },
  64.     QueryInterface : function(aIID)
  65.     {
  66.         if (aIID.equals(Ci.nsIAutoCompleteResult) || aIID.equals(Ci.nsISupports))
  67.         {
  68.             return this;
  69.         }
  70.         throw Cr.NS_ERROR_NO_INTERFACE;
  71.     }
  72. };
  73.  
  74. function samfindAutoCompleteSearch()
  75. {
  76.     this.wrappedJSObject = this;
  77. }
  78.  
  79. samfindAutoCompleteSearch.prototype =
  80. {
  81.     search_results : null,
  82.  
  83.     startSearch : function(searchString, searchParam, result, listener)
  84.     {
  85.         var images = [];
  86.         var comments = [];
  87.         var results = [];
  88.         if (this.search_results)
  89.         {
  90.             for (var i=0; i<this.search_results.length; ++i)
  91.             {
  92.                 if (this.search_results[i].indexOf(searchString) == 0)
  93.                 {
  94.                     images.push(null);
  95.                     comments.push(null);
  96.                     results.push(this.search_results[i]);
  97.                 }
  98.             }
  99.         }
  100.         var newResult = new samfindAutoCompleteResult(searchString,
  101.                                                       Ci.nsIAutoCompleteResult.RESULT_SUCCESS,
  102.                                                       0,
  103.                                                       "",
  104.                                                       images,
  105.                                                       results,
  106.                                                       comments);
  107.         listener.onSearchResult(this, newResult);
  108.     },
  109.  
  110.     stopSearch : function()
  111.     {
  112.     },
  113.  
  114.     QueryInterface : function(aIID)
  115.     {
  116.         if (aIID.equals(Ci.nsIAutoCompleteSearch) || aIID.equals(Ci.nsISupports))
  117.         {
  118.             return this;    
  119.         }
  120.         throw Cr.NS_ERROR_NO_INTERFACE;
  121.     }
  122. };
  123.  
  124. var samfindAutoCompleteSearchModule =
  125. {
  126.     ServiceCID : Components.ID("2af18a90-f5a3-11dd-87af-0800200c9a66"),
  127.     ServiceName : "samfind AutoComplete",
  128.     ServiceContractID : "@mozilla.org/autocomplete/search;1?name=samfind-autocomplete",
  129.  
  130.     registerSelf : function(compMgr, fileSpec, location, type)
  131.     {
  132.         compMgr.QueryInterface(Ci.nsIComponentRegistrar).registerFactoryLocation(this.ServiceCID,
  133.                                                                                  this.ServiceName,
  134.                                                                                  this.ServiceContractID,
  135.                                                                                  fileSpec,
  136.                                                                                  location,
  137.                                                                                  type);
  138.     },
  139.  
  140.     unregisterSelf : function(compMgr, fileSpec, location)
  141.     {
  142.         compMgr.QueryInterface(Ci.nsIComponentRegistrar).unregisterFactoryLocation(this.ServiceCID,
  143.                                                                                    fileSpec);
  144.     },
  145.  
  146.     getClassObject : function(compMgr, cid, iid)
  147.     {
  148.         if (!cid.equals(this.ServiceCID))
  149.         {
  150.             throw Cr.NS_ERROR_NO_INTERFACE;
  151.         }
  152.         if (!iid.equals(Ci.nsIFactory))
  153.         {
  154.             throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  155.         }
  156.         return this.instanceFactory;
  157.     },
  158.  
  159.     canUnload : function(compMgr)
  160.     {
  161.         return true;
  162.     },
  163.  
  164.     singleton : null,
  165.  
  166.     instanceFactory :
  167.     {
  168.         createInstance : function(outer, aIID)
  169.         {
  170.             if (outer != null)
  171.             {
  172.                 throw Cr.NS_ERROR_NO_AGGREGATION;
  173.             }
  174.             if (this.singleton == null)
  175.             {
  176.                 this.singleton = new samfindAutoCompleteSearch();    
  177.             }
  178.             return this.singleton.QueryInterface(aIID);
  179.         }
  180.     }
  181. };
  182.  
  183. function NSGetModule(aCompMgr, aFileSpec)
  184. {
  185.     return samfindAutoCompleteSearchModule;
  186. }